"use strict";
layui.use(["okUtils", "okCountUp"], function () {

    var data = {"time":"2020-03-08",
        "memberCard":221233,
        "privateLesson":12322,
        "featuredLesson":23231,
        "rehabilitationLesson":23232,
        "advertisement":212300
    };

    var countUp = layui.okCountUp;
    var okUtils = layui.okUtils;
    var $ = layui.jquery;

    okLoading.close();

    //在获取页面时，进行ajax访问并获取info数值赋值入data
    $.ajax({
        cache: true,
        type: "POST",
        url:"getIncomeByDate",
        async: false,
        error: function(request) {
            alert("失败!");
        },
        success: function(info) {
            console.log(info);
            data = info;

        }
    });

    var userSourceTodayChartOption = {
        title: {show: false, text: '收入来源', subtext: '纯属虚构', x: 'center'},
        tooltip: {trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)"},
        legend: {orient: 'vertical', left: 'left', data: ['会员卡收入', '私教课收入', '特色课收入', '康复课收入', '广告收入']},
        series: [
            {
                name: '收入来源',
                type: 'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                label: {
                    normal: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        show: true,
                        textStyle: {
                            fontSize: '30',
                            fontWeight: 'bold'
                        }
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data: [{value: data.memberCard, name: '会员卡收入'}, {value: data.privateLesson, name: '私教课收入'}, {value: data.featuredLesson, name: '特色课收入'}, {value: data.rehabilitationLesson, name: '康复课收入'}, {value: data.advertisement, name: '广告收入'}],
                itemStyle: {emphasis: {shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)'}}
            }
        ]
    };

    function changeThis(data){
        userSourceTodayChartOption = {
            title: {show: false, text: '收入来源', subtext: '纯属虚构', x: 'center'},
            tooltip: {trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)"},
            legend: {orient: 'vertical', left: 'left', data: ['会员卡收入', '私教课收入', '特色课收入', '康复课收入', '广告收入']},
            series: [
                {
                    name: '收入来源',
                    type: 'pie',
                    radius: ['50%', '70%'],
                    avoidLabelOverlap: false,
                    label: {
                        normal: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            show: true,
                            textStyle: {
                                fontSize: '30',
                                fontWeight: 'bold'
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data: [{value: data.memberCard, name: '会员卡收入'}, {value: data.privateLesson, name: '私教课收入'}, {value: data.featuredLesson, name: '特色课收入'}, {value: data.rehabilitationLesson, name: '康复课收入'}, {value: data.advertisement, name: '广告收入'}],
                    itemStyle: {emphasis: {shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)'}}
                }
            ]
        };
    }
    /**
     * 今日用户访问来源图表
     */
    function initUserSourceTodayChart() {
        var userSourceTodayChart = echarts.init($("#userSourceTodayChart")[0], "themez");
        userSourceTodayChart.setOption(userSourceTodayChartOption);
        okUtils.echartsResize([userSourceTodayChart]);
    }

    initUserSourceTodayChart();

    $("#query").click(function(){
        var year = +$("#queryYear").val();
        var month = +$("#queryMonth").val();
        var day = +$("#queryDay").val();
        var title = "";
        var dateTime = "";

        if(!isNaN(year)&year>0){
            title = title+year+"年";
            dateTime = dateTime+ year;
            if(month>0&month<13){
                title = title+month+"月";
                dateTime = dateTime +"-"+ month;
                if(day>0&day<judgeDayByMonth(year,month)+1){
                    title = title+day+"日";
                    dateTime = dateTime +"-" + day;
                }else if(day!=0){
                    $("#queryDay").val("");
                    $("#queryDay").attr("placeholder","该日期不存在!");
                    promptfailure();return false;
                }
            }else if(month!=0){
                $("#queryMonth").val("");
                $("#queryMonth").attr("placeholder","该月份不存在!");
                promptfailure();return false;
            }
        }else if(year!=0){
            $("#queryYear").val("");
            $("#queryYear").attr("placeholder","该年份不存在!");
            promptfailure();return false;
        }else{
            $("#queryYear").val("");
            $("#queryYear").attr("placeholder","年份不能为空!");
            promptfailure();return false;
        }
        title = title+"收入占比";
        $.ajax({
            cache: true,
            type: "POST",
            url:"http://localhost:8080/getIncomeByDate",
            data:{"date":dateTime},
            async: false,
            error: function(request) {
                alert(dateTime);
            },
            success: function(info) {
                $("#incomeTitle").html(title);//修改标题
                changeThis(info);//传来的对象,对初始的值进行修改
                initUserSourceTodayChart();//具象化
                promptSuccess();//提示
            }
        });
    });

    //实现成功提示框，且自动消失
    function promptSuccess(){

        $("#info").css("display","block");
        $("#msg").html("<p style='font-size:50px;padding-top:30px;'>成功</p>");
        $("#sign").html("√");
        window.setTimeout(function(){
            $("#info").css("display","none");
        },1000);
    }

    //实现失败提示框，且自动消失
    function promptfailure(){
        $("#info").css("display","block");
        $("#msg").html("<p style='font-size:50px;padding-top:30px;'>失败</p>");
        $("#sign").html("×");
        window.setTimeout(function(){
            $("#info").css("display","none");
        },1000);
    }

    //输入年份月份获取天数
    function judgeDayByMonth(year,month){
        var thisDate = new Date(year,month,0); //当天数为0 js自动处理为上一月的最后一天
        return thisDate.getDate();
    }
});


